
package org.openmrs.module.basicmodule.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.openmrs.Patient;
import org.openmrs.PersonAddress;
import org.openmrs.PersonName;
import org.openmrs.api.context.Context;
import org.openmrs.module.basicmodule.Note;
import org.openmrs.module.basicmodule.NoteService;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


public class MyExamplePortletController implements Controller {
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelMap model = new ModelMap();
        //Integer patientId = (Integer) request.getAttribute("patientId");
        String patientIdStr = (String) request.getParameter("patientId");
        Integer patientId = Integer.valueOf(patientIdStr);
        Patient p = Context.getPatientService().getPatient(patientId);
        System.out.println("MyExamplePortletController handleRequest **************************\n"
                + "Patient id: " + patientIdStr );
        double weight[]=new double[2];
        double d_bp[]=new double[2];
        double s_bp[]=new double[2];
        double temperature[]=new double[2];
        Date date[]=new Date[2];
        int i,j,k,l,m,n;
        i=j=k=l=m=n=0;
        
        List<Encounter> allEncounters= Context.getEncounterService().getEncountersByPatient(p);
        
        for (Encounter encounter:allEncounters)
        {
            if (i<allEncounters.size())
            {
                System.out.println("Here In ENcounter ");
                Set<Obs> allEncounterObs = encounter.getAllObs();                
                date[(n++)%2]=encounter.getEncounterDatetime();                
                for (Obs obs:allEncounterObs)
                {                                                    
                    int temp=obs.getConcept().getConceptId().intValue();
                    System.out.println(temp);
                    switch (temp)
                    {
                        case 5089 : weight[(j++)%2]=obs.getValueNumeric();
                                    break;
                        case 5088 : temperature[(k++)%2]=obs.getValueNumeric();
                                    break;
                        case 5085 : s_bp[(l++)%2]=obs.getValueNumeric();
                                    break;
                        case 5086 : d_bp[(m++)%2]=obs.getValueNumeric();
                                    break;                                        
                    }
                }                                        
                    i++;
                                
            }
            
        }
        System.out.println(" Weights : "+weight.toString()+"\n"+"Temp : "+temperature.toString()+"\n"+s_bp.toString()+"\n"+d_bp.toString());
        
        
                        
        Set<PersonAddress> ad=p.getAddresses();
        PersonName patient_p=p.getPersonName();
        int age=p.getAge();
        String gender=p.getGender();
        //model.put("name",patient_p.getFullName());
        //model.put("age",age);
        //model.put("gender",gender);
        //model.put("encounters",allEncounters);
        
        model.put("message1","This is just a random message");
        model.addAttribute("weight", weight);
        model.addAttribute("temperature", temperature);
        model.addAttribute("s_bp", s_bp);
        model.addAttribute("d_bp", d_bp);
        model.addAttribute("date",date);
        model.put("patient",p);
        
        
        return new ModelAndView("/module/basicmodule/portlets/patientMyExample", model);
    }
    
}
